package com.github.binarywang.demo.wechat.handler;

import com.github.binarywang.demo.wechat.builder.ImageBuilder;
import com.github.binarywang.demo.wechat.builder.TextBuilder;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MenuHandler extends AbstractHandler {

  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                  Map<String, Object> context, WxMpService weixinService,
                                  WxSessionManager sessionManager) {

    String msg = String.format("type:%s, event:%s, key:%s",
        wxMessage.getMsgType(), wxMessage.getEvent(),
        wxMessage.getEventKey());
    if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
     String url = weixinService.oauth2buildAuthorizationUrl("http://antball.free.ngrok.cc/index/","snsapi_userinfo","");

      wxMessage.setUrl(url);

      return null;
    }


    if(wxMessage.getEventKey().equals("btnt")){
      return new ImageBuilder().build("_rQsf9FQOJTWy3GgMDQMRo-qyNt2ApqqezKiLVFg2KD-BmHCX9BILK_8SMnawqOv", wxMessage, weixinService);
    }else if(wxMessage.getEventKey().equals("btnt2")){

//      WxMpXmlOutMessage.NEWS().mediaId(content)
//              .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
//              .build();

      return new ImageBuilder().build("13nDzVsOKhSZhosdIAU_jp3dMExpH2N9d7jAsjkDHv8", wxMessage, weixinService);
    }else{
      return new TextBuilder().build(msg, wxMessage, weixinService);
    }
  }

}
